﻿using System.ComponentModel.DataAnnotations;
using System.Text;

namespace Sharp.Portal.AdminLTE.FormField
{
    /// <summary>
    /// 文本字段
    /// </summary>
    public class TextField : ElementField
    {
        /// <summary>
        /// 文本字段
        /// </summary>
        /// <param name="fieldName"></param>
        public TextField(string fieldName)
        {
            this.FieldName = fieldName;
        }
        /// <summary>
        /// 文本字段
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="type"></param>
        public TextField(string fieldName, DataType type)
        {
            this.FieldName = fieldName;
            Type = type;
        }
        /// <summary>
        /// 数据类型
        /// </summary>
        public DataType Type { get; set; }
        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="nonFirst"></param>
        /// <returns></returns>
        public override string Build(bool nonFirst = false)
        {
            if (this.IsBrowsing() && !this.KeepingXType)
            {
                return $"@Html.Bootstrap().LableFor(m => m.{this.FieldName})" + base.Build(nonFirst);
            }

            var sb = new StringBuilder();
            sb.AppendFormat("@Html.Bootstrap().TextBoxFor(m => m.{0})", FieldName);
            switch (Type)
            {
                case DataType.Date: 
                case DataType.DateTime:
                case DataType.Time:
                    sb.Append(new DateField(FieldName).Build());
                    break;
                case DataType.Password:
                    sb.Append(".Type().Password()");
                    break;
                case DataType.ImageUrl:
                    sb.Append(".Type().Image()");
                    break;
                case DataType.Upload:
                    sb.Append(".Type().File()");
                    break;

                case DataType.EmailAddress:
                    sb.Append(".Type().Email()");
                    break;
                default:
                    break;
            }
            sb.AppendFormat("@Html.ValidationMessageFor(m => m.{0})", FieldName);
            return sb.ToString();
        }
    }
}
